package com.decent.web.system.controller;

import com.decent.common.bo.MessageBean;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.web.system.dto.SystemGoodsDTO;
import com.decent.web.system.service.SystemGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统商品管理接口
 * @author: decent Gjh
 * @date: 2022/10/25/18:33
 * @description:
 */
@RequestMapping("/goods")
@RestController
@Slf4j
@CrossOrigin
public class SystemGoodsController {
    private final SystemGoodsService goodsService;

    public SystemGoodsController(SystemGoodsService goodsService) {
        this.goodsService = goodsService;
    }


    /**
     * 获取商品
     * @param goodsDTO 商品信息
     * @param result result
     * @return 商品列表
     */
    @GetMapping
    public MessageBean selectGoods(@Validated(Select.class) SystemGoodsDTO goodsDTO, BindingResult result){
        if (result.hasErrors()){
            return new MessageBean(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return goodsService.selectGoods(goodsDTO);
    }

    /**
     * 根据id获取商品
     * @param goodsId 商品id
     * @return 商品信息
     */
    @GetMapping("/{id}")
    public MessageBean selectGoodsById(@PathVariable("id") int goodsId){
        return goodsService.selectGoodsById(goodsId);
    }
}
